package com.hd.trans.utils;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ListAdapter;
import android.widget.ListView;


import com.hd.trans.network.PreferenceMgr;
import com.hd.trans.share.TimeUtils;

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;

public class CommonUtil {
    public static final String WINDOW_TITLE = "客服咨询";

    public static short[] bytesToShort(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        short[] shorts = new short[bytes.length / 2];
        ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
        return shorts;
    }

    public static byte[] shortToBytes(short[] shorts) {
        if (shorts == null) {
            return null;
        }
        byte[] bytes = new byte[shorts.length * 2];
        ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(shorts);

        return bytes;
    }


    /**
     * 直接打开客服界面不需要设置其它参数
     *
     * @param context
     */
    public static void openServiceWindow(Context context) {
//        consultService(context, null, null, null);
    }

//    /**
//     *
//     * @param context
//     * @param uri  来源页面的url
//     * @param title  来源页面标题
//     * @param productDetail  来源页面额外信息
//     */
//    public static void consultService(Context context, String uri, String title, ProductDetail productDetail) {
//        YSFUserInfo userInfo = new YSFUserInfo();
//        userInfo.userId = UUID.randomUUID().toString();//保证唯一性即可
//        userInfo.data =  new Gson().toJson(createUserInfo());
//        Unicorn.setUserInfo(userInfo);
//        // 启动聊天界面
//        ConsultSource source = new ConsultSource(uri, title, null);
//        source.productDetail = productDetail;
////        source.groupId=GroupId;
////        source.robotId=MachineService;
//        source.robotFirst=true;
//        openChatWindow(context,WINDOW_TITLE,source);
//    }

//    public static void openChatWindow(Context context,String windowTitle,ConsultSource source){
//        Unicorn.openServiceActivity(context, windowTitle, source);
//    }

    public static long getServiceTimeStamp() {
        String dataString = TimeUtils.getDateToString(PreferenceMgr.getInstance().getServerTimeStamp(),"yyyy年MM月dd日 HH:mm");
        //Log.e("dataString","-----"+dataString+"-----");
        return PreferenceMgr.getInstance().getServerTimeStamp() / 1000;
    }


    public static ArrayList<String> getFilePath(String fileAbsolutePath) {
        ArrayList<String> vecFile = new ArrayList<>();
        File file = new File(fileAbsolutePath);
        File[] subFile = file.listFiles();
        if (subFile != null) {
            for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
                // 判断是否为文件夹
                if (!subFile[iFileLength].isDirectory()) {
                    String filePath = subFile[iFileLength].getAbsolutePath();
                    vecFile.add(filePath);
                }
            }
        }
        return vecFile;
    }

    public static void showKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            view.requestFocus();
            imm.showSoftInput(view, 0);
        }
    }

    public static void hideKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    public static void toggleSoftInput(View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.toggleSoftInput(0, 0);
        }
    }

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        //获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {   //listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);  //计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight();  //统计所有子项的总高度
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        //listView.getDividerHeight()获取子项间分隔符占用的高度
        //params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
}
